<?php

namespace humhub\modules\api\cccstream;

use Yii;
use humhub\modules\content\models\Content;

/**
 * 结合Dashboard和ContentContainer, 获取我的Stream, 包含我参与的Space
 */
class ContentContainerStream3 extends Stream
{

    public $contentContainer;

    public function init()
    {
        parent::init();

        //+- Get Content Container by Param
        if ($this->contentContainer->wall_id != "" 
            && $this->contentContainer->className()=='humhub\modules\user\models\User' 
            && $this->contentContainer->id == Yii::$app->user->id
        ) {

            // User to space memberships
            $spaceMemberships = (new \yii\db\Query())
                    ->select("sm.wall_id")
                    ->from('space_membership')
                    ->leftJoin('space sm', 'sm.id=space_membership.space_id')
                    ->where('space_membership.user_id=' . $this->user->id . ' AND sm.wall_id IS NOT NULL AND space_membership.show_at_dashboard = 1');
            $union = Yii::$app->db->getQueryBuilder()->build($spaceMemberships)[0];

            // Glue together also with current users wall
            $wallIdsSql = (new \yii\db\Query())
                    ->select('wall_id')
                    ->from('user uw')
                    ->where('uw.id=' . $this->user->id);
            $union .= " UNION " . Yii::$app->db->getQueryBuilder()->build($wallIdsSql)[0];

            $this->activeQuery->andWhere('wall_entry.wall_id IN (select subselect.wall_id from (' . $union . ') subselect)');

            /**
             * Begin visibility checks regarding the content container
             */
            $this->activeQuery->leftJoin('wall', 'wall_entry.wall_id=wall.id');
            $this->activeQuery->leftJoin(
                    'space_membership', 'wall.object_id=space_membership.space_id AND space_membership.user_id=:userId AND space_membership.status=:status', ['userId' => $this->user->id, ':status' => \humhub\modules\space\models\Membership::STATUS_MEMBER]
            );

            $condition = ' (wall.object_model=:userModel AND content.visibility=0 AND content.created_by = :userId) OR ';
            // In case of an space entry, we need to join the space membership to verify the user can see private space content
            $condition .= ' (wall.object_model=:spaceModel AND content.visibility = 0 AND space_membership.status = ' . \humhub\modules\space\models\Membership::STATUS_MEMBER . ') OR ';
            $condition .= ' (content.visibility = 1 OR content.visibility IS NULL) ';
            $this->activeQuery->andWhere($condition, [':userId' => $this->user->id, ':spaceModel' => \humhub\modules\space\models\Space::className(), ':userModel' => \humhub\modules\user\models\User::className()]);


            //筛选任务: 1=报名中， 2=进行中，3=已完成
            $p_type = \Yii::$app->request->get('p_type');
            $p1type = \Yii::$app->request->get('p1type');
            if( $p_type==1 && in_array($p1type, array('1','2','3'))){
                //$this->activeQuery->leftJoin('wall', 'wall_entry.wall_id=wall.id');
                $this->activeQuery->leftJoin('space', 'wall.object_model=:spaceModel AND wall.object_id=space.id', ['spaceModel' => \humhub\modules\space\models\Space::className()]);
                $times = time();
                if( $p1type==1 ) $this->activeQuery->andWhere("space.s_t1>$times");
                if( $p1type==2 ) $this->activeQuery->andWhere("space.s_t1<$times AND space.s_t2>$times");
                if( $p1type==3 ) $this->activeQuery->andWhere("space.s_t2<$times");
            }



        } else {
            Yii::warning("No wall id for content container " . get_class($this->contentContainer) . " - " . $this->contentContainer->getPrimaryKey() . " set - stopped stream action!");
            // Block further execution
            $this->activeQuery->andWhere("1=2");
        }
    }
}
